/*
 *  University Of Canterbury - Department of Electrical and Computer Engineering
 *
 *  ENEL323 - Software Engineering 1
 *  Assignment 2: Real-time collision avoidance and anti-locking brake system
 *  File: Auto_Break_System.h
 *
 *  Header file for circular_buffer.c
 *
 *  Authors: Nick Hartley (nijh119) and Andrew Nicholas(adn26)
 *  Last Updated: 22 September 2010
 *
 */

#ifndef CIRC_BUFFER_H
#define CIRC_BUFFER_H

#include "type.h"

#define BUFFER_LENGTH 14
#define START_OF_BUFFER 0

typedef enum boolean {false = 0, true} bool;

typedef enum BUFFER { BUFFER1, BUFFER2 } BUFFER_t;

typedef struct circular_buffer
{
	uint16_t buffer[BUFFER_LENGTH];
	uint16_t *currentptr;
	bool isfull;
			
} buffer_t;

typedef struct double_circular_buffer
{
	 uint16_t buffer1[BUFFER_LENGTH];
	 uint16_t buffer2[BUFFER_LENGTH];
	 
	 uint16_t *currentptr1;
	 uint16_t *currentptr2;
	 
	 bool isfull1;
	 bool isfull2;
	 
	 BUFFER_t read_buffer;
	 BUFFER_t write_buffer;

} dc_buffer_t;

/*
write

Function: Writes a uint16_t to the next position in a double circular buffer.

Inputs: dc_buffer_t pointer to the buffer to be written, uint16_t value to be
		written to the next location in the buffer
Returns: uint16_t, 1 if either of the circular buffers is full, otherwise 0
*/
uint8_t write (dc_buffer_t *buffer, uint16_t value); // writes to current place

/*
read

Function: Reads the next element in the currently full circular buffer.

Inputs: dc_buffer_t pointer to the buffer to be read
Returns: the next uint16_t in the double circular buffer 
*/
uint16_t read (dc_buffer_t *buffer);	// reads next buffer value 


/*
BUFFER_initialisation

Function: Initialises a double circular buffer

Inputs: dc_buffer_t pointer to the buffer to be initialised
Returns: void
*/
void BUFFER_initialisation (dc_buffer_t *buffer);

#endif //circular_buffer.h
